package com.crazy.controller.common;

import java.io.File;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;

import com.crazy.common.ueditor.Uploader;

/**
 * ueditor文本编辑器控制器
 * 
 * @author Zhang Weiwei
 * @since 2013-10-7下午6:12:06
 */
@Controller
@RequestMapping("/ueditor")
public class UeditorController {
	@RequestMapping("/imageUp")
	public void imageUp(HttpServletRequest request,
			HttpServletResponse response, @RequestPart MultipartFile upfile,
			String pictitle) throws Exception {
		response.setContentType("text/html;charset=utf-8");
		try (PrintWriter out = response.getWriter()) {
			ServletContext context = request.getServletContext();
			Uploader up = new Uploader(context);
			String[] fileType = { ".gif", ".png", ".jpg", ".jpeg", ".bmp" };
			up.setAllowFiles(fileType);
			up.setTitle(pictitle);
			up.setOriginalName(upfile.getOriginalFilename());
			up.upload(upfile);
			String str = String.format(
					"{'original':'%s','url':'%s','title':'%s','state':'%s'}",
					up.getOriginalName(), up.getUrl(), up.getTitle(),
					up.getState());
			out.print(str);
		}
	}

	@RequestMapping("/fileUp")
	public void fileUp(HttpServletRequest request,
			HttpServletResponse response, @RequestPart MultipartFile upfile,
			String pictitle) throws Exception {
		response.setContentType("text/html;charset=utf-8");
		try (PrintWriter out = response.getWriter()) {
			ServletContext context = request.getServletContext();
			Uploader up = new Uploader(context);
			String[] fileType = { ".rar", ".doc", ".docx", ".zip", ".pdf",
					".txt", ".swf", ".wmv" }; // 允许的文件类型
			up.setAllowFiles(fileType);
			up.setTitle(pictitle);
			up.setOriginalName(upfile.getOriginalFilename());
			up.upload(upfile);
			String str = String
					.format("{'url':'%s','fileType':'%s','state':'%s','original':'%s'}",
							up.getUrl(), up.getType(), up.getState(),
							up.getOriginalName());
			out.print(str);
		}
	}

	@RequestMapping("/imageManager")
	public void imageManager(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		try (PrintWriter out = response.getWriter()) {
			// 仅做示例用，请自行修改
			String imgStr = "";
			ServletContext context = request.getServletContext();
			String realpath = context.getRealPath("/") + Uploader.fileDir;
			List<File> files = Uploader.getFiles(realpath,
					new ArrayList<File>());
			for (File file : files) {
				imgStr += file.getPath().replace(context.getRealPath("/"), "")
						+ Uploader.separate;
			}
			if (!imgStr.isEmpty()) {
				imgStr = imgStr
						.substring(0, imgStr.lastIndexOf(Uploader.separate))
						.replace(File.separator, "/").trim();
			}
			out.print(imgStr);
		}
	}

	@RequestMapping("/imageDelete")
	public void imageDelete(HttpServletRequest request,
			HttpServletResponse response, @RequestParam String fileName)
			throws Exception {
		response.setContentType("text/html;charset=utf-8");
		try (PrintWriter out = response.getWriter()) {
			ServletContext context = request.getServletContext();
			String realpath = context.getRealPath(fileName);
			File file = new File(realpath);
			String value = file.isFile() ? (file.delete() ? "success" : "error")
					: "error";
			out.print(value);
		}
	}
}
